package com.xbongbong.paas.controller.workflow;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.workflow.explains.dto.WorkflowDynamicManagerDTO;
import com.xbongbong.paas.pojo.workflow.explains.dto.WorkflowFilterConditionFieldDTO;
import com.xbongbong.paas.pojo.workflow.explains.dto.WorkflowInsertNodeOperatorFieldDTO;
import com.xbongbong.paas.pojo.workflow.explains.dto.WorkflowOperatorFieldDTO;
import com.xbongbong.paas.pojo.workflow.explains.dto.WorkflowSortFieldDTO;
import com.xbongbong.paas.pojo.workflow.explains.dto.WorkflowTitleFieldDTO;
import com.xbongbong.paas.pojo.workflow.explains.vo.WorkflowDynamicManagerVO;
import com.xbongbong.paas.pojo.workflow.explains.vo.WorkflowFilterConditionFieldVO;
import com.xbongbong.paas.pojo.workflow.explains.vo.WorkflowOperatorFieldVO;
import com.xbongbong.paas.pojo.workflow.explains.vo.WorkflowSortFieldVO;
import com.xbongbong.paas.pojo.workflow.explains.vo.WorkflowSourceOperatorFieldVO;
import com.xbongbong.paas.pojo.workflow.explains.vo.WorkflowTitleFieldVO;
import com.xbongbong.paas.service.workflow.setting.WorkflowExplainService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/05/25 21:00
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/workflow/explain")
public class WorkflowExplainController {

    @Resource
    private WorkflowExplainService workflowExplainService;

//    @RequestMapping(value = "/conditionNodeFieldList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    public String conditionNodeFieldList(@RequestBody @Valid WorkflowConditionDTO workflowConditionDTO, BindingResult br) {
//        XbbResponse<WorkflowConditionVO> response;
//        if (br.hasErrors()) {
//            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
//            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
//        } else {
//            try {
//                WorkflowConditionVO workflowConditionVO = workflowExplainService.conditionNodeFieldList(workflowConditionDTO);
//                response = new XbbResponse<>(workflowConditionVO);
//            } catch (XbbException e) {
//                response = new XbbResponse<>(e);
//            }
//        }
//        return JSON.toJSONString(response);
//    }

    /**
     * 获取排序字段
     * http://yapi.xbongbong.com/project/231/interface/api/24848
     * @param workflowSortFieldDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/sortFieldList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String sortFieldList(@RequestBody @Valid WorkflowSortFieldDTO workflowSortFieldDTO, BindingResult br) {
        XbbResponse<WorkflowSortFieldVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowSortFieldVO workflowSortFieldVO = workflowExplainService.sortFieldList(workflowSortFieldDTO);
                response = new XbbResponse<>(workflowSortFieldVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * 新建数据节点获取需要更新的来源字段
     * http://yapi.xbongbong.com/project/231/interface/api/24855
     * @param workflowInsertNodeOperatorFieldDTO
     * @return
     * @throws XbbException
     */

    @RequestMapping(value = "/sourceOperatorField", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String sourceOperatorField(@RequestBody @Valid WorkflowInsertNodeOperatorFieldDTO workflowInsertNodeOperatorFieldDTO, BindingResult br) {
        XbbResponse<WorkflowSourceOperatorFieldVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowSourceOperatorFieldVO workflowSourceOperatorFieldVO = workflowExplainService.sourceOperatorField(workflowInsertNodeOperatorFieldDTO);
                response = new XbbResponse<>(workflowSourceOperatorFieldVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 更新节点获取需要更新的字段
     * http://yapi.xbongbong.com/project/231/interface/api/24876
     * @param workflowOperatorFieldDTO
     * @return
     * @throws XbbException
     */

    @RequestMapping(value = "/operatorField", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updateFieldList(@RequestBody @Valid WorkflowOperatorFieldDTO workflowOperatorFieldDTO, BindingResult br) {
        XbbResponse<WorkflowOperatorFieldVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowOperatorFieldVO workflowOperatorFieldVO = workflowExplainService.getOperatorField(workflowOperatorFieldDTO);
                response = new XbbResponse<>(workflowOperatorFieldVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取审批标题的字段
     * http://yapi.xbongbong.com/project/231/interface/api/25149
     * @param workflowTitleFieldDTO
     * @return
     * @throws XbbException
     */

    @RequestMapping(value = "/getTitleField", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getTitleField(@RequestBody @Valid WorkflowTitleFieldDTO workflowTitleFieldDTO, BindingResult br) {
        XbbResponse<WorkflowTitleFieldVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowTitleFieldVO workflowTitleFieldVO = workflowExplainService.getTitleField(workflowTitleFieldDTO);
                response = new XbbResponse<>(workflowTitleFieldVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取过滤条件的字段
     * http://yapi.xbongbong.com/project/231/interface/api/25275
     * @param workflowFilterConditionFieldDTO
     * @return
     * @throws XbbException
     */

    @RequestMapping(value = "/filterConditionField", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String filterConditionField(@RequestBody @Valid WorkflowFilterConditionFieldDTO workflowFilterConditionFieldDTO, BindingResult br) {
        XbbResponse<WorkflowFilterConditionFieldVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowFilterConditionFieldVO workflowTitleFieldVO = workflowExplainService.getFilterConditionField(workflowFilterConditionFieldDTO);
                response = new XbbResponse<>(workflowTitleFieldVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/dynamicManager", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String dynamicManager(@RequestBody @Valid WorkflowDynamicManagerDTO workflowDynamicManagerDTO, BindingResult br) {
        XbbResponse<WorkflowDynamicManagerVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowDynamicManagerVO workflowDynamicManagerVO = workflowExplainService.dynamicManager(workflowDynamicManagerDTO);
                response = new XbbResponse<>(workflowDynamicManagerVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
